<?php

class MySettingPanel extends Qpanel {

    protected $strTemplate;

    protected $mctUser;

    // Controls for User's Data Fields
    protected $lblId;
    public $AuthSys;
    public $lblMsg;
    public $lblUserName;
    public $txtUserName;
    public $txtOldPassword;
    public $txtPassword1;
    public $txtPassword2;

    public $btnSave;

    public function __construct($objParentObject,  $strControlId = null) {
        // Call the Parent
        try {
            parent::__construct($objParentObject, $strControlId);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
        // Setup Template
        $this->strTemplate = __DOCROOT__. __MY__ .  '/tpl/mysettingspanel.tpl.php';

        $this->mctUser = UserMetaControl::Create($this, $_SESSION[_SV_PERM_][_SV_PERM_ID_]);

        $this->lblUserName = $this->mctUser->lblUserName_Create();
        $this->txtUserName = $this->mctUser->txtUserName_Create();

        $this->txtOldPassword = $this->mctUser->txtOldPassword_create();
        $this->txtPassword1 = $this->mctUser->txtPassword1_create();
        $this->txtPassword2 = $this->mctUser->txtPassword2_create();

        $this->lblMsg = new QLabel($this);


        $this->btnSave = new QButton($this);
        $this->btnSave->Name = QApplication::Translate("btnSave");
        $this->btnSave->Text = QApplication::Translate("Change Password");
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click'));
        $this->btnSave->CausesValidation = true;

    }

    public function Validate() {
        $blnToReturn = true;
        //Check Old passord
        $this->AuthSys = new Perm();

        if (strlen($this->txtOldPassword->Text) == 0 ) {
            $blnToReturn=false;
            $this->txtOldPassword->Warning = QApplication::Translate("Required");
        }
        else if ($this->AuthSys->Check_Password($this->lblUserName->Text, $this->txtOldPassword->Text) == false) {
            $blnToReturn=false;
            $this->txtOldPassword->Warning = QApplication::Translate("Wrong Password");
        }

        //Check must be same password 1 and 2
        if  ( strlen($this->txtPassword1->Text) == 0 ) {
            $blnToReturn=false;
            $this->txtPassword1->Warning = QApplication::Translate("Required");
        }
        else if ( strlen($this->txtPassword2->Text) == 0) {
            $blnToReturn=false;
            $this->txtPassword2->Warning = QApplication::Translate("Required");
        }
        else if ( $this->txtPassword1->Text <> $this->txtPassword2->Text ) {
            $blnToReturn=false;
            $this->txtPassword1->Warning = QApplication::Translate("Password does not match");
            $this->txtPassword2->Warning = QApplication::Translate("Password does not match");

        }

        return $blnToReturn;
    }

    public function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the UserMetaControl
        $this->mctUser->SaveUser();
        $this->lblMsg->Text = QApplication::Translate("You have succsfuly changed you password");
        //$this->RedirectToListPage();
    }


}
?>


